import {createAsyncThunk, createSlice} from "@reduxjs/toolkit";
import axios from 'axios';

export const fetchHomeMultiDataAction = createAsyncThunk('fetchHomeMultiDataAction',
    async (extraInfo, {dispatch, getState}) => {
        // extraInfo是组件调用fetchHomeMultiDataAction传的参数
        console.log(extraInfo)
        const res = await axios.get('http://123.207.32.32:8000/home/multidata')
        const {
            data: {
                banner: {list: bannerList = []} = {},
                recommend: {list: recommendList = []} = {}
            } = {}
        } = res.data;
        dispatch(setBanner(bannerList))
        dispatch(setRecommend(recommendList))
        return res.data;
    })

const homeSlice = createSlice({
    name: 'home',
    initialState: {
        banner: [],
        recommend: [],
    },
    reducers: {
        setBanner(state, {payload}) {
            state.banner = payload
        },
        setRecommend(state, {payload}) {
            state.recommend = payload
        },
    },
})

export const {setBanner, setRecommend} = homeSlice.actions;
export default homeSlice.reducer;